package com.vancouver.knows.ui.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.RelatedPostInfo;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.view.AutoGridLinearlayout;

import java.util.List;

/**
 * Created by chenxunlin01 on 2016/4/28.
 */
public class RelatedNewsAdapter extends AutoGridLinearlayout.AutoGridAdapter<RelatedPostInfo>{

    private LayoutInflater inflater;
    private final Context context;

    public RelatedNewsAdapter(Context context, int column, List<RelatedPostInfo> data) {
        super(column, data);
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.business_item_layout, parent, false);
        TextView title = (TextView) rootView.findViewById(R.id.title);
        ImageView singleImage = (ImageView) rootView.findViewById(R.id.single_image);
        LinearLayout attachmentImagesContainer = (LinearLayout) rootView.findViewById(R.id.attachment_imgs_container);
        ImageView imageOne = (ImageView) rootView.findViewById(R.id.post_attachment_img_one);
        ImageView imageTwo = (ImageView) rootView.findViewById(R.id.post_attachment_img_two);
        ImageView imageThree = (ImageView) rootView.findViewById(R.id.post_attachment_img_three);

        rootView.findViewById(R.id.ratingBar).setVisibility(View.GONE);
        rootView.findViewById(R.id.commentCount).setVisibility(View.GONE);
        rootView.findViewById(R.id.distance).setVisibility(View.GONE);

        final RelatedPostInfo relatedPostInfo = mItems.get(position);
        title.setText(relatedPostInfo.getTitle());

        singleImage.setVisibility(View.GONE);
        if (!relatedPostInfo.getAttachmentImages().isEmpty()) {
            attachmentImagesContainer.setVisibility(View.VISIBLE);
            singleImage.setVisibility(View.GONE);
            if (relatedPostInfo.getAttachmentImages().size() > 2) {
                Picasso.with(context).load(relatedPostInfo.getAttachmentImages().get(0)).placeholder(R.drawable.no_image_placeholder).centerCrop()
                        .fit()
                        .into(imageOne);
                Picasso.with(context).load(relatedPostInfo.getAttachmentImages().get(1)).placeholder(R.drawable.no_image_placeholder).centerCrop()
                        .fit()
                        .into(imageTwo);
                Picasso.with(context).load(relatedPostInfo.getAttachmentImages().get(2)).placeholder(R.drawable.no_image_placeholder).centerCrop()
                        .fit()
                        .into(imageThree);
            } else {
                attachmentImagesContainer.setVisibility(View.GONE);
               // singleImage.setVisibility(View.VISIBLE);
               // Picasso.with(context).load(relatedPostInfo.getAttachmentImage()).placeholder(R.drawable.no_image_placeholder).centerCrop()
                //        .fit()
                //        .into(singleImage);
            }
        } else {
//            if (!StringUtil.isEmptyString(relatedPostInfo.getAttachmentImage())) {
//                attachmentImagesContainer.setVisibility(View.GONE);
//                singleImage.setVisibility(View.VISIBLE);
//                Picasso.with(context).load(relatedPostInfo.getAttachmentImage()).placeholder(R.drawable.no_image_placeholder).centerCrop()
//                        .fit()
//                        .into(singleImage);
//            } else {
//                singleImage.setImageResource(R.drawable.no_image_placeholder);
//            }
        }

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CityNewsDetailActivity.class);
                intent.putExtra("postId", relatedPostInfo.getId());
                context.startActivity(intent);
            }
        });
        return rootView;
    }
}
